$(function(){
    var forget_box = new Vue({
        el: "#forget_box",
        data: {
            email: '',
            password: '',
            password_confirmation: '',
            email_code: '',
            clicaptcha_code: '',
            token: "",
            is_email_send: false
        },
        mounted: function() {
            this.token = $('#lcm_lingpai').val()
            // 设置弹窗
            toastr.options = {
                "closeButton": false, // 是否显示关闭按钮
                "debug": false,       // 是否使用debug模式
                "positionClass": "toast-center-center",// 弹出窗的位置
                "showDuration": "500", // 显示的动画时间
                "hideDuration": "500",// 消失的动画时间
                "timeOut": "5000",     // 展现时间
                "extendedTimeOut": "1000",// 加长展示时间
                "showEasing": "swing",    // 显示时的动画缓冲方式
                "hideEasing": "linear",   // 消失时的动画缓冲方式
                "showMethod": "fadeIn",   // 显示时的动画方式
                "hideMethod": "fadeOut"   // 消失时的动画方式
            };
        },
        methods: {
            // 验证邮箱格式
            emial_validate: function() {
                if (this.email === undefined || this.email === '') {
                    return '邮箱不能为空';
                }
                var myReg = /^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;
                if (myReg.test(this.email)) {
                    return true;
                } else {
                    return '邮箱格式错误';
                }
            },
            // 初始化图形验证码
            init_clicaptcha: function () {
                var email_res = this.emial_validate();
                if (email_res !== true) {
                    toastr.error(email_res);
                    return false;
                }

                $('#clicaptcha-submit-info').clicaptcha({
                    src: '/backend/captcha/create',
                    scene_type: 2,
                    callback: function(info){
                        forget_box.clicaptcha_code = info;
                        forget_box.send_email();
                    }
                });
            },
            // 发送邮件
            send_email: function () {
                var email_res = this.emial_validate();
                if (email_res !== true) {
                    toastr.error(email_res);
                    return false;
                }
                $.ajax({
                    method: 'POST',
                    url: '/backend/login/postemail',
                    data: {
                        email: this.email,
                        scene_type: 2,
                        _token: this.token,
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            forget_box.is_email_send = true;
                            toastr.success('邮件发送成功');
                        } else {
                            toastr.error(res.message);
                        }
                    }
                })
            },
            // 提交表单
            submit_forget: function () {
                var email_res = this.emial_validate();
                if (email_res !== true) {
                    toastr.error(email_res);
                    return false;
                }

                if (this.clicaptcha_code == undefined || this.is_email_send == false) {
                    toastr.error('请发送邮件');
                    return false;
                }

                $.ajax({
                    method: 'POST',
                    url: '/backend/login/forgetpost',
                    data: {
                        email: this.email,
                        password: this.password,
                        password_confirmation: this.password_confirmation,
                        email_code: this.email_code,
                        clicaptcha_code: this.clicaptcha_code,
                        _token: this.token,
                    },
                    dataType: 'json',
                    success:(res)=>{
                        if (res.code == 200) {
                            toastr.success('忘记密码成功');
                            window.location.href = '/';
                        } else {
                            toastr.error(res.message);
                        }
                    }
                })
            }
        }
    });
});
